package com.apestech.api.condition.Step;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.stream.ArrayObject;
import com.apestech.framework.xml.condition.modul.ConditionStrategy;

import java.util.ArrayList;

public class StepWhile extends StepPubField {
    private ArrayCallback arrayCallback;
    private StepQuery query=null;
    private StepXlsRead xls=null;
    private JSONArray list=new JSONArray();
    private JSONObject result;

    public StepWhile(ConditionStrategy.Step step, SqlSession session) throws Exception {
        super(step, session);
    }

    public void setArrayCallback(ArrayCallback arrayCallback) {
        this.arrayCallback = arrayCallback;
    }

    public void setResult(JSONObject result) {
        this.result = result;
    }

    public JSONArray getList() {
        return list;
    }

    @Override
    public void exec() throws Exception {
        String type=super.getStep().getType();
        JSONObject out=super.getOut();
        if("sql".equals(type) || "queryid".equals(type)){
            query=new StepQuery(super.getStep(), super.getSession());
            query.setArrayCallback(arrayCallback);
            query.setInput(super.getInput());
            query.setBwhile(true);
            query.exec();
            out=super.getStep().getResultMap();
        } else if("xls".equals(type)){
            xls=new StepXlsRead(super.getStep(), super.getSession());
            xls.setArrayCallback(arrayCallback);
            xls.setInput(super.getInput());
            xls.exec();
        } else if("json".equals(type) || "array".equals(type)){
            JSONArray jlist=null;
            ArrayObject dlist=null;
            JSONObject m=new JSONObject();
            if("json".equals(type)){
                String jsonStr=super.getInput().getString(super.getStep().getField());
                if(jsonStr!=null && !"".equals(jsonStr.trim())){
                    jlist= JSON.toJSONArray(jsonStr);
                    int len=jlist.size();
                    out.put("SYS_JSON_COUNT", len);
                    for(int i=0; i<len; i++){
                        //m.putAll(super.getInput());
                        m.putAll(jlist.get(i));
                        m.putAll(arrayCallback.getRow(m));
                        list.add(m);
                        setArrayRow(i, m, out);
                    }
                    super.getStep().setResultMap(out);
                }
            } else {
                Object obj=super.getInput().get(super.getStep().getField());
                if(obj!=null) {
                    dlist = new ArrayObject(super.getInput().get(super.getStep().getField()));
                    int len=dlist.size();
                    out.put("SYS_JSON_COUNT", len);
                    for(int i=0; i<len; i++){
                        //m.putAll(super.getInput());
                        m.put(super.getStep().getField()+"_I", dlist.getObject(i));
                        m.putAll(arrayCallback.getRow(m));
                        list.add(m);
                        setArrayRow(i, m, out);
                    }
                    super.getStep().setResultMap(out);
                }
            }
        }
        super.setOut(out);
    }


    private void setArrayRow(int i, JSONObject m, JSONObject out) throws Exception {
        ArrayList<String> rklist=null;
        if(result!=null){
            rklist=result.getKeylist();
            int rklen=rklist.size();
            for(int rk=0; rk<rklen; rk++){
                String key=rklist.get(rk);
                String key1=result.getString(key);
                if(key.indexOf("SUM_")==0){
                    if(i==0){
                        out.remove(key);
                    }
                    double sum=0;
                    if(out.get(key)!=null){
                        sum=out.getDouble(key);
                    }
                    if(m.get(key1)!=null){
                        sum=sum+m.getDouble(key1);
                    }
                    out.put(key, sum);
                } else if(key.indexOf("OUT_")==0){
                    out.put(key, key1);
                    String fkey=key.substring(4, key.length());
                    String k=getRealKey(key1, m);
                    if(k!=null){
                        out.put(k, m.get(fkey));
                    }
                }
            }
        }
    }

}
